/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.mallsite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.mybatis.mapper.query.QueryWhere;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.mallsite.modules.shop.entity.GoodsBrand;
import com.mallsite.modules.shop.service.GoodsBrandService;
import com.mallsite.modules.shop.utils.CoreUtils;

/**
 * 商品品牌Controller
 * 
 * @author ruyang
 * @version 2020-12-04
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/goodsBrand")
public class GoodsBrandController extends BaseController {

	@Autowired
	private GoodsBrandService goodsBrandService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public GoodsBrand get(String brandId, boolean isNewRecord) {
		return goodsBrandService.get(brandId, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("shop:goodsBrand:view")
	@RequestMapping(value = { "list", "" })
	public String list(GoodsBrand goodsBrand, Model model) {
		model.addAttribute("goodsBrand", goodsBrand);
		return "modules/shop/goodsBrandList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("shop:goodsBrand:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<GoodsBrand> listData(GoodsBrand goodsBrand, HttpServletRequest request, HttpServletResponse response) {
		if(!CoreUtils.isSysCodeDefault()) {
			QueryWhere qw = goodsBrand.getSqlMap().getWhere();
			String[] shopIds = {UserUtils.getUser().getRefCode(), "0"};
			qw.and("shop_id", QueryType.IN, shopIds);
		}
		Page<GoodsBrand> page = goodsBrandService.findPage(new Page<GoodsBrand>(request, response), goodsBrand);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("shop:goodsBrand:view")
	@RequestMapping(value = "form")
	public String form(GoodsBrand goodsBrand, Model model) {
		model.addAttribute("goodsBrand", goodsBrand);
		model.addAttribute("user", UserUtils.getUser());
		return "modules/shop/goodsBrandForm";
	}

	/**
	 * 保存商品品牌
	 */
	@RequiresPermissions("shop:goodsBrand:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated GoodsBrand goodsBrand) {
		goodsBrandService.save(goodsBrand);
		return renderResult(Global.TRUE, text("保存商品品牌成功！"));
	}

	/**
	 * 删除商品品牌
	 */
	@RequiresPermissions("shop:goodsBrand:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(GoodsBrand goodsBrand) {
		goodsBrandService.delete(goodsBrand);
		return renderResult(Global.TRUE, text("删除商品品牌成功！"));
	}

}